<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程类别" prop="flowTypeId">
        <el-select v-model="queryParams.flowTypeId" placeholder="请选择类别" clearable style="width:120px">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请设备" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请部门" prop="deptId">
        <tree-input v-model="queryParams.deptId" dsCode="sys_dept_tree" style="width:210px;"/>
      </el-form-item>
      <el-form-item label="审核结果" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核结果" clearable style="width:120px">
          <el-option
            v-for="dict in flow_check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="CircleCheck"
          :disabled="multiple"
          @click="batchCheck(1)"
        >批量通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="CircleClose"
          :disabled="multiple"
          @click="batchCheck(2)"
        >批量不通过</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程类别" align="center" prop="flowTypeName" />
      <el-table-column label="设备分类" align="center" prop="typeName" />
      <el-table-column label="申请设备" align="center" prop="deviceCode" />
      <el-table-column label="申请部门" align="center" prop="deptName" />
      <el-table-column label="申请数量" align="center" prop="amount" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="申请原由" align="center" prop="reason" />
      <el-table-column label="审核结果" align="center" prop="checkStatus">
        <template #default="scope">
          <dict-tag :options="flow_check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="流程状态" align="center" prop="flowStatus">
        <template #default="scope">
          <dict-tag :options="flow_status" :value="scope.row.flowStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
          <el-button link type="primary" icon="Select" @click="handleCheck(scope.row)" v-if="scope.row.checkStatus != '1'">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备工单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="flowRef" :model="form" :rules="rules" label-width="80px" :disabled="formAction == 'view'">
        <el-form-item label="流程类别" prop="flowTypeId">
          <el-input v-model="form.flowTypeName" placeholder="请输入类别" readonly/>
        </el-form-item>
        <template v-if="form.flowTypeCode == 'purchase'">
        <el-form-item label="设备分类" prop="typeId">
          <el-input v-model="form.typeName" placeholder="请输入类别" readonly/>
        </el-form-item>
        <el-form-item label="申请数量" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入申请数量" readonly/>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" readonly/>
        </el-form-item>
        </template>
        <el-form-item label="申请设备" prop="deviceId" v-else>
          <el-input v-model="form.deviceId" type="hidden"/>
          <el-input v-model="form.deviceCode" placeholder="请输入选择设备" readonly/>
        </el-form-item>
        <el-form-item label="申请原由" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入申请原由" readonly/>
        </el-form-item>
        <el-form-item label="申请说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入申请说明" readonly/>
        </el-form-item>
        <el-form-item label="流程状态" prop="flowStatus" v-if="formAction != 'check'">
          <el-radio-group v-model="form.flowStatus">
            <el-radio             
              v-for="dict in flow_status" :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核结果" prop="checkStatus">
          <el-radio-group v-model="form.checkStatus">
            <template v-for="dict in flow_check_status" :key="dict.value">
            <el-radio             
              v-if="dict.value !='0'"
              :label="dict.value"
            >{{dict.label}}</el-radio>
            </template>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核人" prop="checkUserName" v-if="formAction != 'check'">
          <el-input v-model="form.checkUserName" placeholder="请输审核人" />
        </el-form-item>
        <el-form-item label="审核说明" prop="checkContent">
          <el-input v-model="form.checkContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="formAction == 'check'">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Flow">
import { listFlow, getFlow, delFlow, checkFlow } from "@/api/base/flow";
import { selectFlowType } from "@/api/base/flowType";
import TreeInput from "@/components/FormInput/Tree";

const { proxy } = getCurrentInstance();
const { flow_status, flow_check_status } = proxy.useDict('flow_status', 'flow_check_status');

const flowList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const typeOptions = ref([]);
const formAction = ref("");


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'desc',
    flowTypeId: null,
    typeId: null,
    deviceCode: null,
    deviceId: null,
    deptId: null,
    amount: null,
    price: null,
    reason: null,
    checkStatus: '0',
    checkUserId: null,
    checkContent: null,
    flowStatus: null,
  },
  rules: {
    flowTypeId: [
      { required: true, message: "流程ID不能为空", trigger: "blur" }
    ],
    typeId: [
      { required: true, message: "设备分类不能为空", trigger: "blur" }
    ],
    deviceId: [
      { required: true, message: "申请设备不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "申请数量不能为空", trigger: "blur" }
    ],
    reason: [
      { required: true, message: "申请原由不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "申请说明不能为空", trigger: "blur" }
    ],
    checkStatus: [
      { required: true, message: "审核结果不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设备工单列表 */
function getList() {
  loading.value = true;
  listFlow(queryParams.value).then(response => {
    flowList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    flowTypeId: null,
    typeId: null,
    deviceCode: null,
    deviceId: null,
    deptId: null,
    amount: null,
    price: null,
    reason: null,
    remark: null,
    checkStatus: null,
    checkUserId: null,
    checkContent: null,
    flowStatus: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null
  };
  proxy.resetForm("flowRef");
  flowType.value = {};
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function changeType(){
  for(let i=0;i<typeOptions.value.length;i++){
    if(typeOptions.value[i].id == form.value.flowTypeId){
      flowType.value = typeOptions.value[i];
      break;
    }
  }
}

function handelSelectDevice(type) {
  selectMode.value = 'radio';
}
function selectedDevice(data){
  form.value.deviceId = data.id;
  form.value.deviceCode = data.name;
  closeSelection();
}
function clearDevice(){
  form.value.deviceId = null;
  form.value.deviceCode = null;
  closeSelection();
}
function closeSelection(){
  selectMode.value = 'none';
  selectType.value = null;
}

/** 新增按钮操作 */
function handleCheck(row) {
  formAction.value = 'check';
    form.value = row;
    open.value = true;
    title.value = "审核设备工单";
}

/** 修改按钮操作 */
function handleView(row) {
  formAction.value = 'view';
    form.value = response.data;
    open.value = true;
    title.value = "设备工单详情";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["flowRef"].validate(valid => {
    if (valid) {
      checkFlow(form.value).then(response => {
        proxy.$modal.msgSuccess("审核成功");
        open.value = false;
        getList();
      });
    }
  });
}

function batchCheck(checkStatus){
  checkFlow({checkStatus:checkStatus, ids:ids.value}).then(response => {
    proxy.$modal.msgSuccess("审核成功");
    open.value = false;
    getList();
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('base/flow/export', {
    ...queryParams.value
  }, `flow_${new Date().getTime()}.xlsx`)
}


getList();
selectFlowType({status:"1"}).then(response=>{
  typeOptions.value = response.rows;
})
</script>
